#include "world.h"
#include "CONSTANTS.h"

world::world(LPDIRECT3DDEVICE9 device):device_(device)
{
}


world::~world(void)
{
	if (XAnimator_ != nullptr)
	{
		XAnimator_ = nullptr;
	}
}

void world::LoadPointers(LPDIRECT3DDEVICE9 _device, IXAnimator *XAnim, ChestsS *ChestArray, int* GameStateP, std::string* MessageP, Player *PlayerP)
{
	device_ = _device;
	XAnimator_ = XAnim;
	GameStateP_ = GameStateP;
	MessageP_ = MessageP;
	ChestArrayP_ = ChestArray;
	PlayerP_ = PlayerP;
}

void world::Render()
{
	terrain.Render();
	skyBox.Render(0.0f,-1000.0f,0.0f,0.0f,20.0f,20.0f,20.0f);
	//D3DCOLOR colour(D3DCOLOR_XRGB(255,255,255));

	//for (int i(0); i < numMesh; i++)
	//{
	//	D3DXVECTOR3 front[4],back[4];
	//	front[0] = BoundingBoxes_[i][0];
	//	front[1] = BoundingBoxes_[i][2];
	//	front[2] = BoundingBoxes_[i][3];
	//	front[3] = BoundingBoxes_[i][1];

	//	back[3] = BoundingBoxes_[i][4];
	//	back[2] = BoundingBoxes_[i][6];
	//	back[1] = BoundingBoxes_[i][7];
	//	back[0] = BoundingBoxes_[i][5];
	//	DBG_GFX.RenderOBBox(front,back,colour);
	//}
}

void world::loadMap(int ID)
{
	terrain.Cleanup();
	skyBox.Cleanup();
	D3DXVECTOR3 minBoundsModelSpace,maxBoundsModelSpace;
	Chests.clear();
	switch(ID)
	{
		case TOWN: 
			{
			terrain.LoadMesh(device_, "worldtest.x");
			skyBox.LoadMesh(device_, "skybox.x");
			collisionMesh.LoadMeshXA(XAnimator_, device_, "towncoll.X");
			Warp warp1(-200.0f, -100, INN, 200, 200, PlayerP_, *this, device_);
			Warps.push_back(warp1);
			Chest chest1(device_, 0, ChestArrayP_, XAnimator_, GameStateP_, MessageP_);
			Chest chest2(device_, 1, ChestArrayP_, XAnimator_, GameStateP_, MessageP_);
			Chests.push_back(chest1);
			Chests.push_back(chest2);
			Chest chest3(device_, 2, ChestArrayP_, XAnimator_, GameStateP_, MessageP_);
			Chests.push_back(chest3);
			break;
			}
		case TESTMAP: 
			{
			terrain.LoadMesh(device_, "testmesh.x");
			skyBox.LoadMesh(device_, "skybox.x");
			collisionMesh.LoadMeshXA(XAnimator_, device_, "testcoll.x");
			break;
			Chest chest3(device_, 2, ChestArrayP_, XAnimator_, GameStateP_, MessageP_);
			Chests.push_back(chest3);
			}
		case INN: 
			{
			terrain.LoadMesh(device_, "inn.x");
			skyBox.LoadMesh(device_, "skyboxindoor.x");
			collisionMesh.LoadMeshXA(XAnimator_, device_, "inncoll.x");
			Chest chest4(device_, 3, ChestArrayP_, XAnimator_, GameStateP_, MessageP_);
			Chests.push_back(chest4);
			break;
			}

	}
	TMeshBounds *indMeshBoundsModelSpace = XAnimator_-> GetBoundingShapes(collisionMesh.meshIdNumber_,&minBoundsModelSpace,&maxBoundsModelSpace,NULL,NULL,&numMesh);
	for (int i(0); i < numMesh; i++)
	{
		D3DXVECTOR3 TempVector[8];
		for (int j(0); j < 8; j++)
		{
			TempVector[j] = indMeshBoundsModelSpace[i].corners[j];
			BoundingBoxes_[i][j].x = TempVector[j].x;
			BoundingBoxes_[i][j].y = TempVector[j].y;
			BoundingBoxes_[i][j].z = TempVector[j].z;
		}
	}
}